package com.yu.shaungxuan.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.shaungxuan.constant.JwtClaimsConstant;
import com.yu.shaungxuan.mapper.TeacherMapper;
import com.yu.shaungxuan.model.common.Result;
import com.yu.shaungxuan.model.dto.TeacherLoginInfo;
import com.yu.shaungxuan.model.entity.ChoiceTeacher;
import com.yu.shaungxuan.model.entity.Relation;
import com.yu.shaungxuan.model.entity.Student;
import com.yu.shaungxuan.model.entity.Teacher;
import com.yu.shaungxuan.properties.JwtProperties;
import com.yu.shaungxuan.service.ChoiceTeacherService;
import com.yu.shaungxuan.service.RelationService;
import com.yu.shaungxuan.service.StudentService;
import com.yu.shaungxuan.service.TeacherService;
import com.yu.shaungxuan.utils.JwtUtil;
import com.yu.shaungxuan.utils.TeacherHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 33522
 * @description 针对表【teacher】的数据库操作Service实现
 * @createDate 2023-12-24 21:12:55
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
        implements TeacherService {

    @Resource
    private JwtProperties jwtProperties;
    @Resource
    private ChoiceTeacherService choiceTeacherService;

    @Resource
    private StudentService studentService;

    @Resource
    private RelationService relationService;

    @Override
    public Result<String> login(TeacherLoginInfo teacherLoginInfo) {
        LambdaQueryWrapper<Teacher> teacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
        teacherLambdaQueryWrapper.eq(StrUtil.isNotBlank(teacherLoginInfo.getUsername()), Teacher::getUsername, teacherLoginInfo.getUsername());
        Teacher teacher = getOne(teacherLambdaQueryWrapper);
        if (teacher == null) {
            return Result.error("用户不存在");
        }
        String password = DigestUtils.md5DigestAsHex(teacherLoginInfo.getPassword().getBytes());
        if (!password.equals(teacher.getPassword())) {
            return Result.error("密码错误");
        }
        //登录成功,生成token
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.TCH_ID, teacher.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        TeacherHolder.saveTeacher(teacher);
        return Result.success(token);
    }

    @Override
    public Result<Page<Student>> show(Integer page, Integer pageSize) {
        Teacher teacher = TeacherHolder.getTeacher();
        if (teacher == null) {
            return Result.error("未登录");
        }
        Page<Student> studentPage = new Page<>(page, pageSize);
        Long id = teacher.getId();
        LambdaQueryWrapper<ChoiceTeacher> choiceTeacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
        choiceTeacherLambdaQueryWrapper.eq(ChoiceTeacher::getTeacherFirstId, id);
        List<ChoiceTeacher> choiceTeachers = choiceTeacherService.list(choiceTeacherLambdaQueryWrapper);
        List<Student> collect = choiceTeachers.stream().map((item -> {
            Long studentId = item.getStudentId();
            LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
            studentLambdaQueryWrapper.eq(Student::getId, studentId);
            return studentService.getOne(studentLambdaQueryWrapper);
        })).collect(Collectors.toList());
        studentPage.setRecords(collect);
        return  Result.success(studentPage);
    }

    @Override
    public Result<String> receive(Long id) {
        Teacher teacher = TeacherHolder.getTeacher();
        if (teacher == null) {
            return Result.error("未登录");
        }
        Student byId = studentService.getById(id);
        if (byId == null){
            return Result.error("学生不存在");
        }
        Relation relation = new Relation();
        relation.setTeacherId(teacher.getId());
        relation.setStudentId(byId.getId());
        relation.setStatus(1);
        relationService.save(relation);
        return Result.success("接收成功");
    }

    @Override
    public Result<String> reject(Long id) {
        Teacher teacher = TeacherHolder.getTeacher();
        if (teacher == null) {
            return Result.error("未登录");
        }
        Student byId = studentService.getById(id);
        if (byId == null){
            return Result.error("学生不存在");
        }
        LambdaQueryWrapper<ChoiceTeacher> choiceTeacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
        choiceTeacherLambdaQueryWrapper.eq(ChoiceTeacher::getTeacherFirstId,teacher.getId());
        choiceTeacherLambdaQueryWrapper.eq(ChoiceTeacher::getStudentId,byId.getId());
        ChoiceTeacher one = choiceTeacherService.getOne(choiceTeacherLambdaQueryWrapper);
        one.setTeacherFirstId(-1L);
        one.setTeacherFirstId(one.getTeacherSecondId());
        one.setTeacherSecondId(one.getTeacherThirdId());
        one.setTeacherThirdId(one.getTeacherFourthId());
        choiceTeacherService.updateById(one);
        return Result.success();
    }
}




